package com.study;

import com.study.bean.StudentService;
import com.study.bean.TeacherService;
import com.study.bean.UserService;
import com.study.configuration.SpringConfiguration;
import com.study.model.Student;
import com.study.model.Teacher;
import com.study.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/*测试AOP*/
public class TestAop {

    public static void main(String[] args) {
        testXml();
        //testXmlAndAnnotation();
        //testAnnotation();
    }

    /*xml方式*/
    public static void testXml() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-xml-annotation.xml");
        //测试Aspect方式
        try {
            UserService userService = context.getBean(UserService.class);
            userService.save(new User());
            userService.save(null);
        } catch (Exception ex) {
        }
        //测试Advisor方式
        StudentService studentService = context.getBean(StudentService.class);
        studentService.save(Student.builder().name("张三").build());
        try {
            studentService.save(null);
        } catch (Exception ex) {
        }
        studentService.save(Student.builder().name("李四").age(1000000).build());
    }

    /*xml与注解方式*/
    public static void testXmlAndAnnotation() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-xml-annotation.xml");
        TeacherService teacherService = context.getBean(TeacherService.class);
        teacherService.save(new Teacher());
        teacherService.save(null);
    }

    /*纯注解方式*/
    public static void testAnnotation() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        TeacherService teacherService = applicationContext.getBean(TeacherService.class);
        teacherService.save(new Teacher());
        teacherService.save(null);
    }


}
